package com.fesine.quartz.chapter3;

import com.fesine.util.FileExtensionFileFilter;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.quartz.*;

import java.io.File;
import java.io.FileFilter;
import java.util.Date;

/**
 * @description: 简单批量任务实现，实现job接口，重写execute方法
 * @author: Fesine
 * @createTime:2016/11/15 19:49
 * @update:修改内容
 * @author: Fesine
 * @updateTime:2016/11/15 19:49
 */
public class ScanDirectoryJob implements Job {

    static Log logger = LogFactory.getLog(ScanDirectoryJob.class);
    public void execute(JobExecutionContext context) throws JobExecutionException {
        //1、每个job都有自己的jobDetail
        JobDetail jobDetail = context.getJobDetail();

        String jobName = jobDetail.getDescription();

        logger.info(jobName + " fire at " + new Date());
        //jobDetail参数获取
        JobDataMap dataMap = jobDetail.getJobDataMap();
        String dirName = dataMap.getString("SCAN_DIR");
        if (dirName == null) {
            throw new JobExecutionException("没有配置路径！");
        }
        File dir = new File(dirName);
        if (!dir.exists()) {
            throw new JobExecutionException("路径不存在！");
        }
        FileFilter fileFilter = new FileExtensionFileFilter(".xml");

        File[] files = dir.listFiles(fileFilter);
        if (files == null || files.length <= 0) {
            logger.info("没有符合条件的文件！");
            return;
        }

        int size = files.length;
        for (int i = 0; i < size; i++) {

            File file = files[i];
            File aFile = file.getAbsoluteFile();
            long fileSize = file.length();
            String msg = aFile+"-Size:"+fileSize;
            logger.info(msg);
        }
    }
}
